home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1997 / HAM Radio 1997.iso / vcls / timetest / timer.pas < prev    next >
Pascal/Delphi Source File  |  1996-04-08  |  6KB  |  212 lines

  1. unit Timer;
  2.  
  3. interface
  4. {  This is a simple program for comparing the execution time of two
  5.    methods.
  6.  
  7.    test_0  is the setup method. Initialize any test arrays or
  8.            conditions.
  9.  
  10.    test_1  is executed by the Test 1 button N Iteration times.
  11.  
  12.    test_2  is executed by the Test 2 button N Iteration times.
  13.  
  14.    Timing accuracy is +/- .055 seconds, so any test should exceed
  15.    a second or two to be meaningful. Test 1 is often 1 tick faster
  16.    than Test 2. I am guessing the events are handled in a consistent
  17.    fashion relative to clock ticks, and so the start time for 1
  18.    is closer to the last tick before it runs. Maybe.
  19.  
  20.    Do several tests in case there are hardware interrupts which
  21.    might invalidate any single test.
  22.  
  23.    This is handy for comparing local vs global variables, near vs far
  24.    calls, and implementation of parts of functions in assembler.
  25.  
  26.    I am new to Delphi, new to Windows, and new to Pascal, any tips
  27.    or constructive criticism would be appreciated.
  28.  
  29.    Placed in the public domain, 1995 by Peter Jennings.
  30.  
  31.    Comments to peterj@netcom.com
  32.  
  33. }
  34.  
  35. uses
  36.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  37.   Forms, Dialogs, StdCtrls, ExtCtrls;
  38.  
  39. type
  40.   TForm1 = class(TForm)
  41.     Label1: TLabel;
  42.     Panel1: TPanel;
  43.     Label2: TLabel;
  44.     Label3: TLabel;
  45.     Single: TLabel;
  46.     Niter: TEdit;
  47.     Plus: TButton;
  48.     Minus: TButton;
  49.     Exit: TButton;
  50.     Test1: TButton;
  51.     Test2: TButton;
  52.     T1Addr: TLabel;
  53.     T2Addr: TLabel;
  54.     Single2: TLabel;
  55.     Total: TLabel;
  56.     Total2: TLabel;
  57.     procedure ExitClick(Sender: TObject);
  58.     procedure PlusClick(Sender: TObject);
  59.     procedure MinusClick(Sender: TObject);
  60.     procedure FormCreate(Sender: TObject);
  61.     procedure Test1Click(Sender: TObject);
  62.     procedure Test2Click(Sender: TObject);
  63.   private
  64.     { Private declarations }
  65.   public
  66.     { Public declarations }
  67.   end;
  68.  
  69. var
  70.   Form1: TForm1;
  71.   NIterations : LongInt;
  72.  
  73.   buf : array[0..8200] of char;
  74.  
  75. function HWToStr(w: Word): String;
  76. procedure test_0;
  77. procedure test_1;
  78. procedure test_2;
  79.  
  80. function StrPosi(var Buffer;Size: word;S: string): integer;
  81.  
  82. implementation
  83.  
  84. {$R *.DFM}
  85.  
  86. procedure TForm1.ExitClick(Sender: TObject);
  87. begin
  88.    Close;
  89. end;
  90.  
  91. procedure TForm1.PlusClick(Sender: TObject);
  92. begin
  93.    NIterations := StrtoInt( NIter.Text );
  94.    if NIterations < 1000000000 then
  95.       NIterations := NIterations * 10;
  96.    NIter.Text := InttoStr(NIterations);
  97. end;
  98.  
  99. procedure TForm1.MinusClick(Sender: TObject);
  100. begin
  101.    NIterations := StrtoInt( NIter.Text );
  102.    NIterations := NIterations div 10;
  103.    If NIterations < 1 then
  104.       NIterations := 1;
  105.    NIter.Text := InttoStr(NIterations);
  106. end;
  107.  
  108. procedure TForm1.FormCreate(Sender: TObject);
  109. begin
  110.    test_0;
  111.    T1Addr.Caption := HWToStr(Seg(test_1)) +':'+ HWToStr(Ofs(test_1));
  112.    T2Addr.Caption := HWToStr(Seg(test_2)) +':'+ HWToStr(Ofs(test_2));
  113. end;
  114.  
  115. procedure TForm1.Test1Click(Sender: TObject);
  116. var
  117.   NIterations : LongInt;
  118.   BeginTime   : TDateTime;
  119.   ElapsedTime : double;
  120.   i           : LongInt;
  121. begin
  122.   NIterations := StrtoInt( NIter.Text );
  123.   Screen.Cursor := crHourGlass;
  124.   Single.Caption := '- - - -';
  125.   total.Caption  := '- - - -';
  126.   Application.ProcessMessages;
  127.   BeginTime := Now;
  128.   for i := 1 to NIterations do
  129.   begin
  130.     Test_1;
  131.   end;
  132.   ElapsedTime := ((Now - BeginTime) * 86400.0);
  133.   total.Caption  := FloatToStrF(ElapsedTime, ffNumber, 4, 2);
  134.   single.Caption := FloatToStrF(ElapsedTime/NIterations, ffNumber, 10,6);
  135.   Screen.Cursor := crDefault;
  136.  
  137. end;
  138.  
  139. procedure TForm1.Test2Click(Sender: TObject);
  140. var
  141.   BeginTime   : TDateTime;
  142.   ElapsedTime : double;
  143.   i           : LongInt;
  144. begin
  145.   NIterations := StrtoInt( NIter.Text );
  146.   Screen.Cursor := crHourGlass;
  147.   Single2.Caption := '- - - -';
  148.   total2.Caption  := '- - - -';
  149.   Application.ProcessMessages;
  150.   BeginTime := Now;
  151.   for i := 1 to NIterations do
  152.   begin
  153.   test_2
  154.   end;
  155.   ElapsedTime := ((Now - BeginTime) * 86400.0);
  156.   total2.Caption  := FloatToStrF(ElapsedTime, ffNumber, 4, 2);
  157.   single2.Caption := FloatToStrF(ElapsedTime/NIterations, ffNumber, 10,6);
  158.   Screen.Cursor := crDefault;
  159.  
  160. end;
  161.  
  162.  
  163. function HWToStr(w: Word): string;
  164. const
  165.    hex: array [0..15] of Char ='0123456789ABCDEF';
  166. var
  167.    H : String;
  168. begin
  169.    HWToStr :=   hex[Hi(w) shr 4] + hex[Hi(w) and $F]
  170.                 + hex[Lo(w) shr 4] + hex[Lo(w) and $F];
  171. end;
  172.  
  173. { ----------------- place the test methods here ----------------------}
  174.  
  175. { This example compares the execution time for a search of an 8K buffer
  176.   using StrPos vs an assembler function. 11.48 vs 6.38 seconds for 12,500
  177.   iterations on a 486/100 laptop.Your mileage may vary.}
  178.  
  179. procedure test_0;      { initialization for test }
  180. var
  181. i : integer;
  182. begin
  183. for i := low(buf) to high(buf) do buf[i] := char(random(26)+ord('a'));
  184. StrCopy(buf+8000,'findme');
  185. end;
  186.  
  187. procedure test_1;      { perform test 1 }
  188. var
  189. p : PChar;
  190. begin
  191. p := StrPos(buf, 'findme');
  192. end;
  193.  
  194. procedure test_2;      { perform test 2 }
  195. var
  196. i : integer;
  197. begin
  198. i := StrPosi(buf,8200,'findme');
  199. end;
  200.  
  201. function StrPosi(var Buffer;Size: word;S: string): integer;
  202. begin
  203. Inline($1E/$16/$1F/$C4/$BE/>buffer/$89/$FB/$8B/$8E/>size/$8D/$B6/>s+2/
  204.        $8A/$86/>s+1/$8A/$96/>s/$84/$D2/$74/$23/$FE/$CA/$30/$F6/$29/$D1/
  205.        $76/$1B/$FC/$F2/$AE/$75/$16/$85/$D2/$74/$0C/$51/$57/$56/$89/$D1/
  206.        $F3/$A6/$5E/$5F/$59/$75/$EC/$89/$F8/$29/$D8/$EB/$02/$31/$C0/
  207.        $89/$46/$FE/$1F)
  208. end;
  209.  
  210.  
  211. end.
  212.